// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets;

import java.nio.ByteBuffer;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoZombie;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.iso.objects.IsoDeadBody;
import zombie.network.GameClient;
import zombie.network.GameServer;
import zombie.network.ServerMap;

public class DeadZombiePacket extends DeadCharacterPacket implements INetworkPacket {
    private byte zombieFlags;
    private IsoZombie zombie;

    @Override
    public void set(IsoGameCharacter character) {
        super.set(character);
        this.zombie = (IsoZombie)character;
        this.zombieFlags = (byte)(this.zombieFlags | (byte)(this.zombie.isCrawling() ? 1 : 0));
    }

    @Override
    public void process() {
        if (this.zombie != null) {
            this.zombie.setCrawler((this.zombieFlags & 1) != 0);
            super.process();
        }
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        super.parse(b, connection);
        if (GameServer.bServer) {
            this.zombie = ServerMap.instance.ZombieMap.get(this.id);
        } else if (GameClient.bClient) {
            this.zombie = GameClient.IDToZombieMap.get(this.id);
        }

        if (this.zombie != null) {
            this.character = this.zombie;
            if (!GameServer.bServer || !this.zombie.isReanimatedPlayer()) {
                this.parseCharacterInventory(b);
                this.parseCharacterHumanVisuals(b);
            }

            this.character.setHealth(0.0F);
            this.character.getHitReactionNetworkAI().process(this.x, this.y, this.z, this.angle);
            this.character.getNetworkCharacterAI().setDeadBody(this);
        } else {
            IsoDeadBody isoDeadBody = this.getDeadBody();
            if (isoDeadBody != null) {
                this.parseDeadBodyInventory(isoDeadBody, b);
                this.parseDeadBodyHumanVisuals(isoDeadBody, b);
            }
        }
    }

    @Override
    public void write(ByteBufferWriter b) {
        super.write(b);
        this.writeCharacterInventory(b);
        this.writeCharacterHumanVisuals(b);
    }

    @Override
    public String getDescription() {
        return super.getDescription() + String.format(" | isCrawling=%b", (this.zombieFlags & 1) != 0);
    }

    public IsoZombie getZombie() {
        return this.zombie;
    }
}
